package com.wissen.new_giskard.server.imp;

import org.hibernate.Query;
import org.hibernate.Session;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.wissen.new_giskard.client.service.AuthenticationService;
import com.wissen.new_giskard.server.domainObjects.User;
import com.wissen.new_giskard.server.hibernate.HibernateUtil;

@SuppressWarnings("serial")

/*
 *    this class test whether user is valid or not
 */
public class AuthenticateServiceimpl  extends RemoteServiceServlet implements AuthenticationService {

	@Override
	public boolean authenticateUser(String username, String password)
			throws IllegalArgumentException {
		boolean result=false;
		
		/*
		 *  create object of hibernate
		 */
		HibernateUtil hibernateUtil = new HibernateUtil();
		
	  try
	  {
		   /*
		    * create session
		    */
		  Session session = hibernateUtil.getSession();
		  
		  /*
		   *  excute query
		   */
			Query userValidationQuery = session
					.createQuery("from User user where user.name = :username and user.password = :password");
			userValidationQuery.setParameter("username", username);
			userValidationQuery.setParameter("password", password);
			User user = (User) userValidationQuery.uniqueResult();
			hibernateUtil.commit();
			/*
			 *  user is not null then valid user
			 */
			if(user!=null)    
				result=true;
	  }
	  catch (Exception e) {
		  
		  /*
		   *  rollback if exception occur
		   */
            hibernateUtil.rollback();
	}
		
		
		
		return result;
	}
}

